<?php

App::uses('AppController', 'Controller');

class KontaktController extends AppController {
    
    public $uses = array('Contact');
	
	public $pageTitle = 'Kontakt';

    public function index() {
    }
    
    public function send() {
        $data = @$this->data['Contact'];
        if (! empty($data)) {
            
            $this->Contact->create($data);
            
            if (! $this->Contact->validates()) {
                $this->set('data', $data);
                $this->setFlash('Uneti podaci nisu validni!');
                return $this->render('index');
            }
            
            $to      = $this->settingsData['email'];
            $subject = "Kontakt sa sajta: " . $data['name'] . ", " . $data['email'];
            $message = $data['message'];
            
            $server  = preg_replace('/^w+\./', '', $_SERVER['SERVER_NAME']);
            $headers = array(
                "From: {$server}<no-reply@{$server}>",
                "Reply-To: " . $data['name'] . "<" . $data['email'] . ">",
                "Content-Type: text/html; charset=utf-8"
            );
            
            if (mail($to, $subject, $message, implode("\r\n", $headers) . "\r\n")) {
                $this->setFlash('Poruka je uspešno prosleđena', 'ok');
            } else {
                $this->setFlash('Greška pri slanju poruke. Molimo pokušajte ponovo.');
                $this->set('data', $data);
                return $this->render('index');
            }
        }
        
        $this->redirect('index');
    }

}

?>